//
//  DetailView.m
//  Comma
//
//  Created by 张诗雨 on 15/1/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailView.h"
#import "Masonry.h"
#import "GlobalMethod.h"
#import "ExpressionView.h"
#import "SETextView.h"
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

@interface DetailView ()<SETextViewDelegate>

@end

@implementation DetailView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

#pragma mark - 底部视图
// 创建底部视图
- (void)createShare
{
    [self setBackgroundColor:[UIColor whiteColor]];
    [self createShareView];
    [self createFooterButton];
    [self createShareButton];
#warning 注册消息中心
    [self registerForKeyboardNotifications];
    // 表情区域
    self.expressionView = [[ExpressionView alloc] init];
    [self addSubview:_expressionView];
    self.coverView = [[UIButton alloc] initWithFrame:CGRectMake(0, - SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [_coverView addTarget:self action:@selector(coverViewAction:) forControlEvents:UIControlEventTouchUpInside];
    [_coverView setBackgroundColor:[UIColor clearColor]];
    
    [self addSubview:_coverView];
}


- (void)addWhiteView
{
    self.whiteView = [[UIView alloc] initWithFrame:self.frame];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.whiteView];
}
- (void)createFooterButton
{
    CGFloat side = (SCREEN_WIDTH - 96 - 49 * 3) / 3;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - self.footerView.frame.size.height, SCREEN_WIDTH, 0.5)];
    view.backgroundColor = LINEERGRAY;
    [self addSubview:view];
    //赞
    self.supportButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.supportButton setFrame:CGRectMake(24, self.footerView.frame.size.height - 49, 49, 49)];
    [self.supportButton setBackgroundImage:[UIImage imageNamed:@"Button_Praise"] forState:UIControlStateNormal];
    [self.footerView addSubview:self.supportButton];
    //踩
    self.demeanButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.demeanButton setBackgroundImage:[UIImage imageNamed:@"Button_Belittle"] forState:UIControlStateNormal];
    [self.footerView addSubview:self.demeanButton];
    [self.demeanButton setFrame:CGRectMake(self.supportButton.frame.origin.x + self.supportButton.frame.size.width + side, self.supportButton.frame.origin.y, 49, 49)];
    //分享加号
    self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.shareButton setBackgroundImage:[UIImage imageNamed:@"Button_share"] forState:UIControlStateNormal];
    [self.footerView addSubview:self.shareButton];
    [self.shareButton setFrame:CGRectMake(self.demeanButton.frame.origin.x + self.demeanButton.frame.size.width + side, self.supportButton.frame.origin.y, 49, 49)];
    //评论
    self.writeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.writeButton addTarget:self action:@selector(writeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.writeButton setBackgroundImage:[UIImage imageNamed:@"Button_Comment"] forState:UIControlStateNormal];
    [self.footerView addSubview:self.writeButton];
    [self.writeButton setFrame:CGRectMake(self.shareButton.frame.origin.x + self.shareButton.frame.size.width + side, self.supportButton.frame.origin.y, 49, 49)];
    
}

- (void)createShareView
{
    self.shareView = [UIView new];
    self.shareView.backgroundColor = [UIColor blackColor];
    self.shareView.alpha = 0.7;
    [self.shareView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 176)];
    [self addSubview:self.shareView];
    
    //键盘上方view
    self.writeView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 44)];
    [_writeView setBackgroundColor:FOOTERGRAY];
    [self addSubview:_writeView];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(8, 6, SCREEN_WIDTH - 96, 32)];
    [self.writeView addSubview:self.scrollView];
    
    
    self.writeTextField = [[SETextView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 96, 32)];
    [_writeTextField setBackgroundColor:[UIColor whiteColor]];
    [_writeTextField setDelegate:self];
    [_writeTextField setFont:[UIFont fontWithName:@"Helvetica" size:17.0f]];
    [_writeTextField setEditable:YES];
    [_scrollView addSubview:_writeTextField];
    
    
    
    self.expressionButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 72, 11, 22, 22)];
    [self.expressionButton addTarget:self action:@selector(expressionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.expressionButton setBackgroundImage:[UIImage imageNamed:@"Button_addExpression"] forState:UIControlStateNormal];
    [self.writeView addSubview:self.expressionButton];
    
    self.senderButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 44, 11, 35, 22)];
    self.senderButton.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    
    [self.senderButton setTitle:@"发送" forState:UIControlStateNormal];
    [self.senderButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [self.writeView addSubview:self.senderButton];
    
}






- (void)createShareButton
{
    WS(weakSelf);
    CGSize size = CGSizeMake(125 / 2, 125 / 2);
    //朋友圈
    self.FriendCircleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.FriendCircleButton setFrame:CGRectMake((SCREEN_WIDTH - 54 * 3 - 31 * 2) / 2, 14, 54, 51)];
    [self.FriendCircleButton setBackgroundImage:[UIImage imageNamed:@"button_pengyouquan"] forState:UIControlStateNormal];
    [self.shareView addSubview:self.FriendCircleButton];
    
    //微信
    self.weiXinButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.weiXinButton setFrame:CGRectMake(self.FriendCircleButton.frame.origin.x + self.FriendCircleButton.frame.size.width + 31, 14, 54, 51)];
    [self.weiXinButton setBackgroundImage:[UIImage imageNamed:@"button_wechat"] forState:UIControlStateNormal];
    [self.shareView addSubview:self.weiXinButton];
    
    //新浪
    self.XinLangButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.XinLangButton setFrame:CGRectMake(self.weiXinButton.frame.origin.x + self.weiXinButton.frame.size.width + 31, 14, 54, 51)];
    [self.XinLangButton setBackgroundImage:[UIImage imageNamed:@"button_weibo"] forState:UIControlStateNormal];
    [self.shareView addSubview:self.XinLangButton];
    
    //QQ空间
    self.QQZineButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.QQZineButton setFrame:CGRectMake(self.FriendCircleButton.frame.origin.x, 76, 54, 51)];
    [self.QQZineButton setBackgroundImage:[UIImage imageNamed:@"button_QQzone"] forState:UIControlStateNormal];
    [self.shareView addSubview:self.QQZineButton];
    
    // QQ
    self.QQButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.QQButton setFrame:CGRectMake(self.QQZineButton.frame.origin.x + self.QQZineButton.frame.size.width + 31, 76, 54, 51)];
    [self.QQButton setBackgroundImage:[UIImage imageNamed:@"button_QQ"] forState:UIControlStateNormal];
    [self.shareView addSubview:self.QQButton];
    
    // 人人
    self.renrenButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.renrenButton setFrame:CGRectMake(self.QQButton.frame.origin.x + self.QQButton.frame.size.width + 31, 76, 54, 51)];
    [self.renrenButton setBackgroundImage:[UIImage imageNamed:@"button_renren"] forState:UIControlStateNormal];
    [self.shareView addSubview:self.renrenButton];
    
    
    //关闭按钮,关闭分享视图
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Button_share"]];
    img.transform = CGAffineTransformMakeRotation(45 * M_PI / 180.0);
    img.userInteractionEnabled = YES;
    [self.shareView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.demeanButton.mas_right).with.offset(45);
        make.bottom.equalTo(weakSelf.shareView.mas_bottom).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(49, 49));
    }];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.shareView addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.demeanButton.mas_right).with.offset(40);
        make.bottom.equalTo(weakSelf.shareView.mas_bottom).with.offset(0);
        make.size.mas_equalTo(size);
    }];
}

#pragma mark - 底部视图结束

#pragma mark - 主视图
// 创建tableHeaderView
- (void)createTableHeaderView
{
    //    WS(weakSelf);
    self.tableHeaderView  = [[UIView alloc] initWithFrame:CGRectZero];
    //头像
    [self.tableHeaderView setBackgroundColor:[UIColor whiteColor]];
    self.headPic = [UIImageView new];
    self.headPic.userInteractionEnabled = YES;
    self.headPic.frame = CGRectMake(8, 8, 32, 32);
    self.headPic.layer.cornerRadius = 16;
    self.headPic.clipsToBounds = YES;
    [self.tableHeaderView addSubview:self.headPic];
    //    [self.headPic mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(_tableHeaderView.mas_left).with.offset(8);
    //        make.top.equalTo(_tableHeaderView.mas_top).with.offset(8);
    //        make.width.mas_equalTo(@(32));
    //        make.height.mas_equalTo(@(32));
    //    }];
    //类型
    self.typeLable = [UILabel new];
    self.typeLable.numberOfLines = 0;
    _typeLable.textColor = REDCOLOR;
    //    _typeLable.font = [UIFont systemFontOfSize:14.0f];
    [_typeLable setFont:[UIFont fontWithName:@"Helvetica" size:14.0f]];
    _typeLable.frame = CGRectMake(self.headPic.frame.origin.x + self.headPic.frame.size.width + 8, 9, 44, 34);
    [self.tableHeaderView addSubview:self.typeLable];
    //    [self.typeLable mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(_tableHeaderView.mas_top).with.offset(17);
    //        make.left.equalTo(_headPic.mas_right).with.offset(8);
    //        make.width.mas_equalTo(@44);
    //        make.height.mas_equalTo(@(28));
    //    }];
    //标题
    self.titleLable = [UILabel new];
    self.titleLable.numberOfLines = 2;
    [_titleLable setTextColor:TEXTGREENCOLOR];
    //    [_titleLable setFont:[UIFont boldSystemFontOfSize:14.0f]];
    [_titleLable setFont:[UIFont fontWithName:@"Helvetica" size:14.0f]];
    [_titleLable setFont:[UIFont boldSystemFontOfSize:14]];
    [_titleLable setTextAlignment:NSTextAlignmentCenter];
    [self.tableHeaderView addSubview:_titleLable];
    //    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(_tableHeaderView.mas_top).with.offset(17);
    //        make.left.equalTo(_typeLable.mas_right).with.offset(0);
    //        make.width.mas_equalTo(@(SCREEN_WIDTH - 100));
    //        make.height.mas_equalTo(@(28));
    //    }];
    //发布者
    self.puberNick = [UILabel new];
    //    self.puberNick.font = [UIFont boldSystemFontOfSize:11.0f];
    [_puberNick setFont:[UIFont fontWithName:@"Helvetica" size:11.0f]];
    [_puberNick setFont:[UIFont boldSystemFontOfSize:11.0f]];
    [self.puberNick setNumberOfLines:0];
    [self.tableHeaderView addSubview:self.puberNick];
    
    //点点控制格式
    self.dian = [[UILabel alloc] initWithFrame:CGRectZero];
    //    self.dian.font = [UIFont systemFontOfSize:11.0f];
    [self.dian setFont:[UIFont fontWithName:@"Helvetica" size:11.0f]];
    self.dian.textColor = TEXTERGRAY;
    [self.tableHeaderView addSubview:self.dian];
    
    //发布者身份
    self.puberRole = [UILabel new];
    self.puberRole.font = [UIFont systemFontOfSize:11.0f];
    [self.puberRole setFont:[UIFont fontWithName:@"Helvetica" size:11.0f]];
    
    self.puberRole.textColor = TEXTERGRAY;
    [self.tableHeaderView addSubview:self.puberRole];
    
    //发布时间
    self.pubTime = [UILabel new];
    self.pubTime.font = [UIFont systemFontOfSize:11.0f];
    [self.pubTime setFont:[UIFont fontWithName:@"Helvetica" size:11.0f]];
    self.pubTime.textColor = TEXTERGRAY;
    self.pubTime.textAlignment = NSTextAlignmentRight;
    [self.tableHeaderView addSubview:self.pubTime];
    
    //内容详情
    self.contentView = [UIWebView new];
    _contentView.scalesPageToFit = YES;
    //    _contentView.userInteractionEnabled = NO;
    _contentView.scrollView.scrollEnabled = NO;
    [_tableHeaderView addSubview:self.contentView];
    
    // 踩
    self.badImageView = [[UIImageView alloc] init];
    self.badImageView.image = [UIImage imageNamed:@"Mark_Belittlesmall"];
    [_tableHeaderView addSubview:self.badImageView];
    self.badLable = [[UILabel alloc] init];
    [self.badLable setFont:[UIFont systemFontOfSize:11]];
    [self.badLable setFont:[UIFont fontWithName:@"Helvetica" size:11.0f]];
    [_tableHeaderView addSubview:self.badLable];
    
    // 赞
    self.goodImageView = [[UIImageView alloc] init];
    self.goodImageView.image = [UIImage imageNamed:@"Mark_Praisesmall"];
    [_tableHeaderView addSubview:self.goodImageView];
    self.goodLable = [[UILabel alloc] init];
    [self.goodLable setFont:[UIFont systemFontOfSize:11]];
    [self.goodLable setFont:[UIFont fontWithName:@"Helvetica" size:11.0f]];
    [_tableHeaderView addSubview:self.goodLable];
}

- (void)createTableView
{
    self.commentTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 113) style:UITableViewStyleGrouped];
    //    self.commentTabelView.separatorStyle = NO;
    self.commentTabelView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.commentTabelView];
}


#pragma mark -
#pragma mark SETextViewDelegate
//- (void)editTextFinishedValue:(NSString *)endValue
//{
//    NSLog(@"value === %@", endValue);
//}

//- (void)textViewDidChange:(SETextView *)textView
//{
//    [self updateLayout];
//}
//
//
//- (void)updateLayout
//{
//    BOOL change = NO;
//    
//    CGSize containerSize = self.scrollView.frame.size;
//    CGSize contentSize = [self.writeTextField sizeThatFits:containerSize];
//    
//    CGRect frame = self.writeTextField.frame;
//    frame.size.height = MAX(contentSize.height, containerSize.height);
//    
//    self.writeTextField.frame = frame;
//    self.scrollView.contentSize = frame.size;
//    
//    
//    
//    if (self.beforeHeight == frame.size.height) {
//        change = NO;
//        [self.scrollView scrollRectToVisible:self.writeTextField.caretRect animated:YES];
//    } else {
//        self.beforeHeight = frame.size.height;
//        change = YES;
//    }
//    
//    
//    if (frame.size.height < 100 && frame.size.height > 32 && change) {
//        
//        
//        [self.writeView setFrame:CGRectMake(self.writeView.frame.origin.x, self.writeView.frame.origin.y - self.beforeHeight + self.scrollView.frame.size.height, self.writeView.frame.size.width, self.writeView.frame.size.height + self.beforeHeight - self.scrollView.frame.size.height)];
//        self.scrollView.frame = CGRectMake(self.scrollView.frame.origin.x, self.scrollView.frame.origin.y, self.scrollView.frame.size.width, frame.size.height);
//        self.writeTextField.frame = CGRectMake(self.writeTextField.frame.origin.x, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
//    }
//    
//    
//}



#pragma mark - 几个点击事件
// 返回按钮 ,关闭分享视图
- (void)backButtonAction:(id)sender
{
    [UIView animateWithDuration:0.25 animations:^{
        self.shareView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.shareView.frame.size.height);
        self.shareButton.transform = CGAffineTransformMakeRotation(0);
        self.shareButton.hidden = NO;
    } completion:^(BOOL finished) {
        
    }];
}

// 评论的点击事件
- (void)writeButtonAction:(id)sender
{
    
    [self.writeView setFrame:CGRectMake(0, SCREEN_HEIGHT - 44, SCREEN_WIDTH, self.writeView.frame.size.height)];
    
    [self.writeTextField beginFirstResponder];
}
// 表情视图的点击事件
- (void)expressionButtonAction:(id)sender
{
    if (_beginEditExp) {
        [self.writeTextField beginFirstResponder];
        [_expressionView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, _expressionView.height)];
        self.beginEditExp = NO;
    }
    else {
        self.beginEditExp = YES;
        [self.writeTextField endEditing:YES];

        CGFloat height = _expressionView.height;
        [UIView animateWithDuration:0.3 animations:^{
            self.writeView.frame = CGRectMake(0, SCREEN_HEIGHT - height - self.writeView.frame.size.height, SCREEN_WIDTH, self.writeView.frame.size.height);
            [self.expressionView setFrame:CGRectMake(0, SCREEN_HEIGHT - _expressionView.height, SCREEN_HEIGHT, _expressionView.height)];
        } completion:^(BOOL finished) {
            
            [self.coverView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - self.writeView.frame.origin.y)];
        }];
    }
}

- (void)coverViewAction:(id)sender
{
    [self setUserInteractionEnabled:NO];
    [UIView animateWithDuration:0.5 animations:^{
        [self.writeView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.writeView.frame.size.height)];
        [self.expressionView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, _expressionView.height)];
    } completion:^(BOOL finished) {
        [self.coverView setFrame:CGRectMake(0, - SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self setUserInteractionEnabled:YES];
    }];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

#pragma mark - 几个点击事件结束

#pragma mark - 键盘事件
- (void)registerForKeyboardNotifications

{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardDidHideNotification object:nil];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)keyboardWasShown:(NSNotification *)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    self.keyBoardHeight = kbSize.height;
    self.keyBoardDidShow = YES;
    self.keyBoardAnimationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    //    if (self.editingTextField.frame.origin.y == _writeTextField.frame.origin.y) {
    [self viewWillChangeByKeyBoard];
    //    }
    self.beginEditExp = NO;
//    [self.writeTextField beginFirstResponder];
    [_expressionView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, _expressionView.height)];
    self.writeView.userInteractionEnabled = YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.editingTextField = textField;
}

- (void)viewWillChangeByKeyBoard
{
    [UIView animateWithDuration:self.keyBoardAnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [self.writeView setFrame:CGRectMake(0, SCREEN_HEIGHT - self.keyBoardHeight - 44 - self.writeView.frame.size.height + 44, SCREEN_WIDTH, self.writeView.frame.size.height)];
    } completion:nil];
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
    //[UIView animateWithDuration:self.keyBoardAnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
    if (self.beginEditExp == NO) {
        self.writeView.userInteractionEnabled = NO;
        [self.writeView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.writeView.frame.size.height)];
    }
    
    //    } completion:^(BOOL finished) {
    //
    //    }];
    
}

#pragma mark - 键盘事件结束

- (void)lineView:(CGPoint)point  view:(UIView *)add
{
    UIView *view = [UIView new];
    view.backgroundColor = DIVIDINGLINECOLOR;
    view.frame = CGRectMake(point.x, point.y, SCREEN_WIDTH, 0.5);
    [add addSubview:view];
}

@end
